/**
 * <strong>Title : WorkUnitTypeController.java<br>
 * </strong> <strong>Package : cn.b.sky.mes.platform.web.controller<br>
 * </strong> <strong>Description : </strong>@工作单元类型控制器@<br>
 * <strong>Create on : 2015-9-10 上午09:57:19<br>
 * </strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人 | 修改日期 | 修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */
package cn.b.sky.mes.cm.controller;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.cm.model.WorkUnit;
import cn.b.sky.mes.cm.model.WorkUnitType;
import cn.b.sky.mes.cm.model.condition.WorkUnitCondition;
import cn.b.sky.mes.cm.model.condition.WorkUnitTypeCondition;
import cn.b.sky.mes.cm.service.WorkUnitService;
import cn.b.sky.mes.cm.service.WorkUnitTypeService;
import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;

/**
 * <strong>Title : WorkUnitTypeController.java<br></strong>
 * <strong>Package : cn.b.sky.mes.platform.web.controller<br></strong>
 * <strong>Description : </strong>作业单元类型控制器<br> 
 * <strong>Create on : 2015-9-10 上午09:57:19<br></strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人 | 修改日期 | 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */

@Controller
@RequestMapping(value = "/workUnitType/*")
public class WorkUnitTypeController extends SecurityController {
  @Resource
  private WorkUnitTypeService workUnitTypeService;

  @Resource
  private WorkUnitService workUnitService;

  @RequestMapping(value = "workUnitTypeList")
  public String workUnitTypeList(Model model, @ModelAttribute("condition") WorkUnitTypeCondition workUnitTypeCondition,
      String navTabId) {
    workUnitTypeCondition.setSiteCode(getSiteCode());
    List<WorkUnitType> workUnitTypes = workUnitTypeService.selectWorkUnitTypeList(workUnitTypeCondition);
    int count = workUnitTypeService.getWorkUnitTypeCount(workUnitTypeCondition);
    workUnitTypeCondition.setData(workUnitTypes);
    workUnitTypeCondition.setTotalCount(count);
    model.addAttribute("page", workUnitTypeCondition);
    model.addAttribute(NAVTABID, navTabId);
    return "cm/workUnitType/workUnitType_list";
  }

  @RequestMapping(value = "addWorkUnitType")
  public String addWorkUnitType(String navTabId, Model model) {
    WorkUnitCondition condition = new WorkUnitCondition();

    condition.setLockFlag(0); //查询未锁定用户
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("pattern", "add");

    return "cm/workUnitType/workUnitType_add";
  }

  @RequestMapping(value = "selectWorkUnit")
  public String selectWorkUnit(String navTabId, @ModelAttribute("condition") WorkUnitCondition condition, Model model) {
    condition.setSiteCode(getSiteCode());
    condition.setLockFlag(0);
    condition.setOrderField("workUnitCode");
    List<WorkUnit> workUnits = workUnitService.selectWorkUnitList(condition);
    int count = workUnitService.getWorkUnitCount(condition);
    model.addAttribute("pattern", "add");
    condition.setData(workUnits);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    model.addAttribute(NAVTABID, navTabId);

    return "cm/workUnitType/workUnit_select";
  }

  @RequestMapping(value = "modifyWorkUnitType")
  public String modifyWorkUnitType(String navTabId, String id, Model model) {
    WorkUnitType workUnitType = workUnitTypeService.getWorkUnitType(id, getSiteCode());

    List<WorkUnit> workUnits = workUnitTypeService.selectAssignByUnitType(id, getSiteCode());
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("workUnitType", workUnitType);
    model.addAttribute("pattern", "modify");
    model.addAttribute("workUnits", workUnits);
    return "cm/workUnitType/workUnitType_add";

  }

  @RequestMapping(value = "viewWorkUnitType")
  public String viewWorkUnitType(String navTabId, String id, Model model) {

    WorkUnitType workUnitType = workUnitTypeService.getWorkUnitType(id, getSiteCode());
    List<WorkUnit> workUnits = workUnitTypeService.selectAssignByUnitType(id, getSiteCode());
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("workUnitType", workUnitType);
    model.addAttribute("pattern", "read");
    model.addAttribute("workUnits", workUnits);
    return "cm/workUnitType/workUnitType_add";

  }

  @RequestMapping(value = "validateWorkUnitTypeCode")
  @ResponseBody
  public boolean validateWorkUnitTypeCode(String workUnitTypeCode, Model model) {
    if (!StringUtils.isBlank(workUnitTypeCode)) {
      WorkUnitTypeCondition workUnitTypeCondition = new WorkUnitTypeCondition();
      workUnitTypeCondition.setWorkUnitTypeCode(workUnitTypeCode);
      workUnitTypeCondition.setSiteCode(SecurityContextUtil.getSiteCode());
      int count = workUnitTypeService.getWorkUnitTypeExactCount(workUnitTypeCondition);
      return count == 0;
    }
    return true;
  }

  @RequestMapping(value = "saveWorkUnitType")
  @ResponseBody
  public DWZResult saveWorkUnitType(Model model, String navTabId, String ids, String id, WorkUnitType workUnitType,
      HttpServletRequest request) {
    if (workUnitType == null) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }
    setCreateInfo(workUnitType);
    //作业单元类型id
    if (StringUtils.isBlank(id)) {
      workUnitType.setSiteCode(getSiteCode());
      workUnitTypeService.insertWorkUnitType(workUnitType);

      if (StringUtils.isNotBlank(ids)) {
        String[] workUnitIds = ids.split(",");//作业单元id数组
        if (workUnitIds.length > 0) {
          workUnitTypeService.insertWorkUnitTypeAssign(workUnitType, workUnitIds, getSiteCode());
        }
      }

      String forwardUrl = getForwardUrl(request) + "/workUnitType/workUnitTypeList?navTabId=" + navTabId;
      return dialogAjaxDone(getImeViewMessageI18n(null, "successfulOperation.M"), navTabId, null, forwardUrl, null);
    } else {
      workUnitTypeService.updateWorkUnitType(workUnitType);
      if (StringUtils.isNotBlank(ids)) {
        String[] workUnitIds = ids.split(",");//作业单元id数组
        if (workUnitIds.length > 0) {
          workUnitTypeService.updateWorkUnitTypeAssign(workUnitType, workUnitIds, getSiteCode());
        }
      } else {
        workUnitTypeService.deleterAssignByUnitType(workUnitType, getSiteCode());
      }
      return dialogAjaxDone(getImeViewMessageI18n(null, "successfulOperation.M"), navTabId, null, null, null);
    }

  }

  @RequestMapping("deleteWorkUnitType")
  @ResponseBody
  public DWZResult deleteWorkUnitType(Model model, String navTabId, String id, HttpServletRequest request) {
    String ids = request.getParameter("ids");
    if (StringUtils.isBlank(ids)) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }
    String[] workUnitTypes = ids.split(",");
    int len = workUnitTypes.length;
    if (len == 0) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }
    BatchJobReport[] reports = new BatchJobReport[len];
    boolean success = true;
    for (int i = 0; i < len; i++) {
      reports[i] = workUnitTypeService.deleteWorkUnitTypes(workUnitTypes[i], getSiteCode());
      if (!reports[i].isSuccess()) {
        success = false;
      }
    }
    if (success) {
      //直接返回成功消息
      return navTabAjaxDone(navTabId, null, null, null, getImeViewMessageI18n(null, "successfulOperation.M"), null);
    } else {
      return batchjobAjaxDone(request, Arrays.asList(reports));
    }

  }

}
